var Date_func={
	datePad:function(number){
		number=String(number);
		number=(number.length==1)?"0"+number:number;
		return number;
	},
	dateTrim:function(str){
		var str=String(str);
		if(str.length==2&&str[0]==0)
			return parseInt(str[1]);
		return str;
	},
	toUnix:function(year,month,day,hour,minute,second){
		var hour=hour||0;
		var minute=minute||0;
		var second=second||0;
		var now = new Date(year,month-1,day,hour,minute,second);
		return now.getTime(); 
	},
	datetimeToUnix:function(datetime){
		if(!datetime) return Date.parse();
		var datetime=String(datetime);
		var tmp= datetime.replace(/:/g,'-');
		tmp=tmp.replace(/ /g,'-');
		var tmp = tmp.split("-");
		tmp[3]=(tmp[3]==undefined)?0:tmp[3];
		tmp[4]=(tmp[4]==undefined)?0:tmp[4];
		tmp[5]=(tmp[5]==undefined)?0:tmp[5];
		
		return this.toUnix(this.dateTrim(tmp[0]),this.dateTrim(tmp[1]),this.dateTrim(tmp[2]),this.dateTrim(tmp[3]),this.dateTrim(tmp[4]),this.dateTrim(tmp[5]));
	},
	dateToUnix:function(date){
		return this.datetimeToUnix(date);
	},
	unixToDatetime:function(unix){
		var unix=unix||(new Date()).getTime();
		var time = new Date(parseInt(unix)); 
		return time.getFullYear()+"-"+this.datePad((time.getMonth()+1))+"-"+this.datePad(time.getDate())+" "+this.datePad(time.getHours())+":"+this.datePad(time.getMinutes())+":"+this.datePad(time.getSeconds());
	},
	unixToDate:function(unix){
		return this.unixToDatetime(unix).substr(0,10);
	},
	dateWeek:function(date){
		var time=this.dateToUnix(date);
		var d=new Date();
		d.setTime(time);
		return d.getDay();
	},
	selectDate:function(datetype,begintime,endtime){
		var time=new Date();
		switch(datetype){
			case "":
				begintime.value="";
				endtime.value="";
				break;
			case "today":
				endtime.value=begintime.value=this.unixToDate();
				break;
			case "yesterday":
					begintime.value=endtime.value=this.unixToDate(time.getTime()-24*3600*1000);
				break
			case "week":
				var weak=time.getDay();
				if(weak==0){ weak=7;}//0代表星期日
				var t1=time.getTime()-(weak-1)*24*3600*1000;
				var t2=time.getTime()+(7-weak)*24*3600*1000;
				begintime.value=this.unixToDate(t1);
				endtime.value=this.unixToDate(t2);
				break;
			case "lastweek":
				var weak=time.getDay();
				if(weak==0){ weak=7;}
				var t1=time.getTime()-(weak+6)*24*3600*1000;
				var t2=time.getTime()-weak*24*3600*1000;
				begintime.value=this.unixToDate(t1);
				endtime.value=this.unixToDate(t2);
				break;
			case "month":
				var days=new Date(time.getFullYear(),time.getMonth()+1,0);
				var monthdays=days.getDate();	
					begintime.value=this.unixToDate((time.getTime()-(time.getDate()-1)*24*3600*1000));	
					endtime.value=begintime.value.substr(0,8)+monthdays;
				break;
			case "lastmonth":
				var year=time.getFullYear();
				var	month=time.getMonth();
					if(month==0){
						month=11;
						year--;
					}else{
						month--;
					}
				var mytime=new Date(year,month,1);
				begintime.value=mytime.getFullYear()+"-"+this.datePad((mytime.getMonth()+1))+"-01";
				endtime.value=mytime.getFullYear()+"-"+this.datePad((mytime.getMonth()+1))+"-"+
				this.datePad((new Date(time.getFullYear(),time.getMonth(),0).getDate()));
				break;
		}
	}
}